import QtQuick 2.0

import "weather.js" as WorldWeather
import "zipcode.js" as ZipLocate

Item {
    id: weather
    anchors.top: parent.top
    anchors.topMargin: 5
    anchors.left: parent.left
    anchors.leftMargin: 5
    width: parent.width - 10
    height: parent.height - 10
    
    property string zipcode: "60657"
    property string tempUnit: "F"
    property string distUnit: "mi/ft/in"
    property string speedUnit: "mph"
    
    function updateLocation() {
        ZipLocate.post2citystate(zipcode, "US", location);
        updateCurrentWeather();
        update5DayForecast();
    }
    
    function updateCurrentWeather() {
        WorldWeather.updateWeather(zipcode, weather.tempUnit, weather.distUnit, weather.speedUnit, currentImg, temp, weatherDesc, humidity, precipitation, visibility, cloud, pressureBar, pressure, windCompass, windSpeed);
    }
    
    function update5DayForecast() {
        WorldWeather.update5DayForcast(zipcode, weather.tempUnit, dow1, date1, img1, highTemp1, lowTemp1, dow2, date2, img2, highTemp2, lowTemp2, dow3, date3, img3, highTemp3, lowTemp3, dow4, date4, img4, highTemp4, lowTemp4, dow5, date5, img5, highTemp5, lowTemp5);
    }
    
    function updateDistanceUnits() {
        
    }
    
    Column {
        anchors.top: parent.top
        anchors.left: parent.left
        width: parent.width
        height: parent.height
        spacing: 30
        
        Component.onCompleted: {
            ZipLocate.post2citystate(zipcode, "US", location);
        }
    
        Item {
            id: tempNpic
            height: 300
            width: parent.width
            Text {
                id: location
                anchors.top: parent.top
                anchors.topMargin: 15
                anchors.left: parent.left
                anchors.leftMargin: 25
                text: "-"
                color: "#FF000000"
                font { family: "Verdana"; pointSize: 18; }
            }
            Row {
                anchors.top: parent.top
                anchors.topMargin: 10
                anchors.left: parent.left
                anchors.leftMargin: 25
                Text {
                    id: temp
                    text: "-"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 200; }
                }
                // Space //
                Item {
                    width: 20
                    height: parent.height
                }
                // Space //
                Text {
                    id: tempUnit
                    anchors.top: parent.top
                    anchors.topMargin: 40
                    text: "\u00B0"+weather.tempUnit
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 50; bold: true; }
                }
            }
            Image {
                id: currentImg
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 490
                width: 216
                height: 216
            }
            Text {
                id: weatherDesc;
                anchors.right: currentImg.right
                anchors.top: parent.top
                anchors.topMargin: 250
                text: "-"
                color: "#FF000000"
                font { family: "Verdana"; pointSize: 30; }
            }
        }
        Item {
            id: otherStats
            width: parent.width
            height: 250
            Column {
                id: otherStatNum
                anchors.top: parent.top
                anchors.topMargin: 25
                anchors.left: parent.left
                anchors.leftMargin: 25
                spacing: 15
                Text {
                    id: humidity;
                    text: "-"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: precipitation;
                    text: "-"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: cloud;
                    text: "-"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: visibility;
                    text: "-"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
            }
            Column {
                id: otherStatDesc
                anchors.top: parent.top
                anchors.topMargin: 25
                anchors.left: parent.left
                anchors.leftMargin: 160
                spacing: 15
                Text {
                    id: humidityTxt;
                    text: "Humidity"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: precipitationTxt;
                    text: "Precipitation"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: cloudTxt;
                    text: "Cloud Cover"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
                Text {
                    id: visibilityTxt;
                    text: "Visibility"
                    color: "#FF000000"
                    font { family: "Verdana"; pointSize: 30; }
                }
            }
            Column {
                id: windNpressure
                anchors.top: parent.top
                anchors.topMargin: 25
                anchors.left: parent.left
                anchors.leftMargin: 450
                spacing: 15
                Row {
                    spacing: 25
                    Item {
                        width: 14
                        height: 92 
                    }
                    Rectangle {
                        id: pressureBar
                        width: 20
                        height: 92
                        border { width: 2; color: "#FF000000"; }
                        gradient: Gradient {
                            GradientStop { position: 0.0000; color: "#00FFFFFF" }
                            GradientStop { position: 0.2999; color: "#00FFFFFF" }
                            GradientStop { position: 0.3000; color: "#FFC63322" }
                            GradientStop { position: 1.0000; color: "#FFC63322" }
                        }
                    }
                    Item {
                        width: 6
                        height: 92 
                    }
                    Column {
                        spacing: 5
                        anchors.top: parent.top
                        anchors.topMargin: 0
                        Text {
                            id: pressureTxt
                            text: "Pressure"
                            color: "#FF000000"
                            font { family: "Verdana"; pointSize: 30; }
                        }
                        Text {
                            id: pressure
                            text: "  -"
                            color: "#FF000000"
                            font { family: "Verdana"; pointSize: 30; }
                        }
                    }
                }
                Row {
                    spacing: 25
                    Image {
                        id: windCompass
                        width: 92
                        height: 92
                    }
                    Column {
                        spacing: 5
                        anchors.top: parent.top
                        anchors.topMargin: 10
                        Text {
                            id: windTxt
                            text: "Wind"
                            color: "#FF000000"
                            font { family: "Verdana"; pointSize: 30; }
                        }
                        Text {
                            id: windSpeed;
                            text: "  -"
                            color: "#FF000000"
                            font { family: "Verdana"; pointSize: 30; }
                        }
                    }
                }
            }
        }
        Item {
            id: forcast
            width: parent.width
            height: 228
            Column {
                id: day1forcast
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 25
                spacing: 5
                Row {
                    spacing: 7
                    Text {
                        id: dow1
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: date1
                        text: "-/-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Image {
                    id: img1
                    width: 88
                    height: 88
                }
                Row {
                    spacing: 5
                    Text {
                        id: highTemp1Txt
                        text: "high:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: highTemp1
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Row {
                    spacing: 5
                    Text {
                        id: lowTemp1Txt
                        text: "low:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: lowTemp1
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
            }
            Column {
                id: day2forcast
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 185
                spacing: 5
                Row {
                    spacing: 7
                    Text {
                        id: dow2
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: date2
                        text: "-/-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Image {
                    id: img2
                    width: 88
                    height: 88
                }
                Row {
                    spacing: 5
                    Text {
                        id: highTemp2Txt
                        text: "high:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: highTemp2
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Row {
                    spacing: 5
                    Text {
                        id: lowTemp2Txt
                        text: "low:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: lowTemp2
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
            }
            Column {
                id: day3forcast
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 345
                spacing: 5
                Row {
                    spacing: 7
                    Text {
                        id: dow3
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: date3
                        text: "-/-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Image {
                    id: img3
                    width: 88
                    height: 88
                }
                Row {
                    spacing: 5
                    Text {
                        id: highTemp3Txt
                        text: "high:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: highTemp3
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Row {
                    spacing: 5
                    Text {
                        id: lowTemp3Txt
                        text: "low:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: lowTemp3
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
            }
            Column {
                id: day4forcast
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 505
                spacing: 5
                Row {
                    spacing: 7
                    Text {
                        id: dow4
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: date4
                        text: "-/-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Image {
                    id: img4
                    width: 88
                    height: 88
                }
                Row {
                    spacing: 5
                    Text {
                        id: highTemp4Txt
                        text: "high:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: highTemp4
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Row {
                    spacing: 5
                    Text {
                        id: lowTemp4Txt
                        text: "low:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: lowTemp4
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
            }
            Column {
                id: day5forcast
                anchors.top: parent.top
                anchors.topMargin: 20
                anchors.left: parent.left
                anchors.leftMargin: 665
                spacing: 5
                Row {
                    spacing: 7
                    Text {
                        id: dow5
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: date5
                        text: "-/-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Image {
                    id: img5
                    width: 85
                    height: 85
                }
                Row {
                    spacing: 5
                    Text {
                        id: highTemp5Txt
                        text: "high:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: highTemp5
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
                Row {
                    spacing: 5
                    Text {
                        id: lowTemp5Txt
                        text: "low:"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                    Text {
                        id: lowTemp5
                        text: "-"
                        color: "#FF000000"
                        font { family: "Verdana"; pointSize: 18; }
                    }
                }
            }
        }
    }
    Column {
        anchors.top: parent.top
        anchors.topMargin: 20
        anchors.left: parent.left
        anchors.leftMargin: 850
        spacing: 15
        Text {
            anchors.horizontalCenter: parent.horizontalCenter
            font { family: "Verdana"; pointSize: 30 }
            text: "Live Radar"
            color: "#FF000000"
        }
        Image {
            id: liveRadar
            source: "http://radar.weather.gov/Conus/RadarImg/latest.gif"
            width: 800
            height: 500
        }
    }
    
    Timer {
        interval: 10000 // 10 seconds
        running: true
        repeat: true
        triggeredOnStart: true
        onTriggered: {
            WorldWeather.updateWeather(zipcode, weather.tempUnit, weather.distUnit, weather.speedUnit, currentImg, temp, weatherDesc, humidity, precipitation, visibility, cloud, pressureBar, pressure, windCompass, windSpeed);
            WorldWeather.update5DayForcast(zipcode, weather.tempUnit, dow1, date1, img1, highTemp1, lowTemp1, dow2, date2, img2, highTemp2, lowTemp2, dow3, date3, img3, highTemp3, lowTemp3, dow4, date4, img4, highTemp4, lowTemp4, dow5, date5, img5, highTemp5, lowTemp5);
        }
    }
    Timer {
        interval: 300000 // 5 minutes (300 seconds)
        running: true
        repeat: true
        triggeredOnStart: false
        onTriggered: {
            liveRadar.source = "images/blank.png"
            liveRadar.source = "http://radar.weather.gov/Conus/RadarImg/latest.gif";
        }
    }

    Timer {
        interval: 1 // 5 minutes (300 seconds)
        running: true
        repeat: false
        triggeredOnStart: false
        onTriggered: {
            zipcode = settings.getValue("settings/" + userSelection.loggedUser + "/zipcode");
            if( zipcode === "" ) zipcode = "60657";
            WorldWeather.updateWeather(zipcode, weather.tempUnit, weather.distUnit, weather.speedUnit, currentImg, temp, weatherDesc, humidity, precipitation, visibility, cloud, pressureBar, pressure, windCompass, windSpeed);
            WorldWeather.update5DayForcast(zipcode, weather.tempUnit, dow1, date1, img1, highTemp1, lowTemp1, dow2, date2, img2, highTemp2, lowTemp2, dow3, date3, img3, highTemp3, lowTemp3, dow4, date4, img4, highTemp4, lowTemp4, dow5, date5, img5, highTemp5, lowTemp5);
        }
    }
}
